package com.choosefile;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

import jxl.read.biff.BiffException;

import com.readexcel.ReadExcel;

public class YFileChooser implements ActionListener, FocusListener, KeyListener {
	JFrame frame = new JFrame("文件选择");
	JTabbedPane tabPane = new JTabbedPane();// 选项卡布局
	Container con = new Container();// 布局1
	JLabel label1 = new JLabel("初始规则号");
	JLabel label2 = new JLabel("选择文件");
	JTextField text1 = new JTextField();
	JTextField text2 = new JTextField();
	JButton button2 = new JButton("选择excel文件");
	JButton submit = new JButton("确定");
	JFileChooser jfc = new JFileChooser();// 文件选择器

	YFileChooser() {
		jfc.setCurrentDirectory(new File("E:\\"));// 文件选择器的初始目录定为d盘
		// 下面两行是取得屏幕的高度和宽度
		double lx = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		double ly = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		frame
				.setLocation(new Point((int) (lx / 2) - 150,
						(int) (ly / 2) - 150));// 设定窗口出现位置
		frame.setSize(455, 250);// 设定窗口大小
		frame.setContentPane(tabPane);// 设置布局
		// 下面设定标签等的出现位置和高宽
		label1.setBounds(10, 90, 100, 30);
		label1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		text1.setBounds(100, 90, 120, 30);
		text1.addKeyListener(this);
		label2.setBounds(10, 30, 100, 30);
		text2.setBounds(100, 30, 120, 30);
		text2.addFocusListener(this);
		text2.addKeyListener(this);
		button2.setBounds(230, 30, 150, 30);
		submit.setBounds(310, 90, 70, 30);
		submit.addKeyListener(this);
		submit.setEnabled(false);
		button2.addActionListener(this);// 添加事件处理
		submit.addActionListener(this);
		label2.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		con.add(label1);
		con.add(text1);
		con.add(label2);
		con.add(text2);
		con.add(button2);
		con.add(submit);
		con.add(jfc);
		tabPane.setFont(new Font("Menu.font", Font.PLAIN, 20));
		tabPane.setForeground(Color.RED);// 设置标签前景色
		tabPane.add("本程序不支持excel 2010，仅支持excel 97-2003", con);// 添加布局1
		frame.setVisible(true);// 窗口可见
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 使能关闭窗口，结束程序
	}

	public void actionPerformed(ActionEvent e) {// 事件处理
		if (e.getSource().equals(button2)) {
			jfc.setFileSelectionMode(0);// 设定只能选择到文件
			int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
			if (state == 1) {
				return;// 撤销则返回
			} else {
				File f = jfc.getSelectedFile();// f为选择到的文件
				text2.setText(f.getAbsolutePath());
			}
			validate();
		}
		if (e.getSource().equals(submit)) {
			ReadExcel read = new ReadExcel();
			String xlsfile = text2.getText().trim();
			String number = text1.getText().trim();
			xlsfile = xlsfile.replaceAll("\\\\", "\\/");
			File file = new File(xlsfile);
			int rulernumber = Integer.parseInt(number);
			read.setRulernumber(rulernumber);
			try {
				read.readExcel(file, 1);
			} catch (BiffException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			frame.dispose();
		}

	}

	public void validate() {
		if (validateXLS() && validateNO()) {
			submit.setEnabled(true);
		} else
			submit.setEnabled(false);
	}

	public boolean validateXLS() {
		String xlsfile = text2.getText().trim();
		String reg = "\\.xlsx$|\\.xls$";
		Pattern pattern = Pattern.compile(reg);
		Matcher m = pattern.matcher(xlsfile);
		boolean isXLS = m.find();
		return isXLS;
	}

	public boolean validateNO() {
		String number = text1.getText().trim();
		String reg = "\\d*";
		Pattern pattern = Pattern.compile(reg);
		Matcher m = pattern.matcher(number);
		boolean isNO = m.matches();
		return isNO;
	}

	public static void main(String[] args) {
		new YFileChooser();
	}

	public void focusGained(FocusEvent e) {

	}

	public void focusLost(FocusEvent e) {
		if (e.getSource().equals(text2)) {
			validate();
		}
	}

	public void keyPressed(KeyEvent e) {

		if (e.getSource().equals(text1)) {
			boolean isxls = validateXLS();
			if (!isxls) {
				JOptionPane.showMessageDialog(null, "对不起，请选择xls文件");
			}
			validate();
		}
		if (e.getSource().equals(text2)) {
			validate();
		}
	}

	public void keyReleased(KeyEvent e) {
		if (e.getSource().equals(text1)) {

			boolean isNO = validateNO();
			if (!isNO) {
				JOptionPane.showMessageDialog(null, "对不起，请输入一个数字");
			}
			validate();
		}
		if (e.getSource().equals(text2)) {
			validate();
		}
	}

	public void keyTyped(KeyEvent e) {
	}
}
